local skill = fk.CreateSkill{
  name = "tea__hanlv",
  tags = { Skill.Compulsory }
}
local U = require "packages/utility/utility"
---@type TrigSkelSpec<TrigFunc>
local hanlv = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(skill.name) then return end
    if event == fk.DamageInflicted then
      return data.damage > 1 and player:getMark("tea__hanlv_mark") == 0
    else
      if data.card.is_damage_card then
        return #data.tos == 1 and #data:getExtraTargets() > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      player.room:notifySkillInvoked(player, skill.name, "defensive")
		  player:broadcastSkillInvoke(skill.name, 2)
      data.damage = 1
    else
      local room = player.room
      room:notifySkillInvoked(player, skill.name, "defensive")
		  player:broadcastSkillInvoke(skill.name, table.random({1, 2}))
      local targets = data:getExtraTargets() ---@type ServerPlayer[]
      local to = room:askToChoosePlayers(player, {
        skill_name = skill.name,
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#tea__hanlv_choose:::" .. data.card:toLogString(),
        cancelable = false,
      })
      if #to == 1 then
        data:addTarget(to[1])
        room:sendLog{ type = "#AddTargetsBySkill", from = player.id, to = {to[1].id}, arg = skill.name, arg2 = data.card:toLogString() }
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local hanlv_re = {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skill.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, skill.name, "special")
		player:broadcastSkillInvoke(skill.name, 1)
    player.room:setPlayerMark(player, "tea__hanlv_mark", 1)
  end,
}
skill:addEffect(fk.DamageInflicted, hanlv)
skill:addEffect(fk.CardUsing, hanlv)
skill:addEffect(fk.AfterDrawPileShuffle, hanlv_re)
return skill